package com.yjxxt.crud;

import java.sql.*;

public class DButil {
    static String url = "jdbc:mysql://localhost:3306/user";
    static String username="root";
    static  String password="a123123";
    static{
        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //2.获取连接对象
    public static Connection getConnection() throws SQLException {
        return   DriverManager.getConnection(url,username,password);
    }

    public static void main(String[] args) throws SQLException {
        Connection connection = DButil.getConnection();
        if(connection!=null){
            System.out.println("连接成功");
        }

    }

    public static void close(Connection conn, Statement sta, ResultSet rs) {
        try {
            if (null != rs) {
                rs.close();
                rs = null;
            }
            if (null != sta) {
                sta.close();
                sta = null;
            }
            if (null != conn && !conn.isClosed()) {
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
